﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Controls;
using Microsoft.LightSwitch.Threading;
using System.Windows;
namespace LightSwitchApplication
{
    public partial class WebCrawlingHistoriesList
    {
        private DatePicker _startDate = null;
        private DatePicker _endDate = null;
        private DataGrid _grid;

        partial void StartQuery_Execute()
        {
            DateTime? curStartDate = null;
            DateTime? curEndDate = null; 
            Dispatchers.Main.EnsureInvoke(() =>
            {
                if (_startDate != null && _endDate != null)
                {
                    curStartDate = _startDate.SelectedDate;
                    curEndDate = _endDate.SelectedDate;
                }
            }, true);

            if (curStartDate.HasValue && curEndDate.HasValue)
            {
                this.WebCrawlingHistoryfromTime = new DateTime(curStartDate.Value.Year, curStartDate.Value.Month, curStartDate.Value.Day, 0, 0, 0);
                this.WebCrawlingHistorytoTime = new DateTime(curEndDate.Value.Year, curEndDate.Value.Month, curEndDate.Value.Day, 23, 59, 59);
            }

            //问题：已经将EndDate的控件设置成23：59：59这样的时间，不过查询结果似乎还是按照当前的时间部分查询
            this.QueryByUpdateTime.Refresh();
        }

        void StartDate_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            _startDate = e.Control as DatePicker;
            _startDate.SelectedDateChanged += _startDate_SelectedDateChanged;
            _startDate.SelectedDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);            
        }

        void _startDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (WebCrawlingHistoryfromTime != _startDate.SelectedDate.Value)
                this.WebCrawlingHistoryfromTime = _startDate.SelectedDate.Value;
        }

        void EndDate_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            _endDate = e.Control as DatePicker;

            _endDate.SelectedDateChanged += _endDate_SelectedDateChanged;

            var firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
            var nextMonthFirstDay = firstDay.AddMonths(1);
            var lastDay = nextMonthFirstDay.AddDays(-1);
            var day=new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);                               
            _endDate.SelectedDate = day;            
        }

        void _endDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.WebCrawlingHistorytoTime != _endDate.SelectedDate.Value)
                this.WebCrawlingHistorytoTime = _endDate.SelectedDate.Value;
        }

        void GridSelected_ControlAvailable(object sender, ControlAvailableEventArgs e)
        {
            _grid = e.Control as DataGrid;
            if (_grid != null)
            {
                _grid.SelectionMode = DataGridSelectionMode.Extended;
            }
            
        }

        partial void gridDeleteSelected_Execute()
        {            
            if (_grid.SelectedItems.Count > 0)
            {
                if (this.ShowMessageBox("您确定要删除所选的数据吗？", "系统提示", MessageBoxOption.YesNo) == MessageBoxResult.No)
                {
                    return;
                }

                foreach (WebCrawlingHistory item in _grid.SelectedItems)
                {
                    item.Delete();
                }
                //this.Save();                  //自动保存有时候会出错，提示集合已经修改
            }
            else
            {
                this.ShowMessageBox("请选择要删除的行！", "系统提示", MessageBoxOption.Ok);
            }
        }

        partial void WebCrawlingHistoriesList_Created()
        {
            this.FindControl("StartDate").ControlAvailable += StartDate_ControlAvailable;
            this.FindControl("EndDate").ControlAvailable += EndDate_ControlAvailable;

            var control = this.FindControl("grid");
            control.ControlAvailable += GridSelected_ControlAvailable;
        }
    }
}
